# configs/config_manager.py
# 统一的配置管理模块

import yaml
import json
from pathlib import Path
from typing import Dict, Any, Optional


class ConfigManager:
    """统一的配置管理器"""
    
    def __init__(self, config_dir: Optional[Path] = None):
        self.config_dir = Path(config_dir or __file__).parent
        self._cache: Dict[str, Any] = {}
    
    def load_config(self, config_name: str) -> Dict[str, Any]:
        """加载配置文件"""
        if config_name in self._cache:
            return self._cache[config_name]
        
        # 尝试加载 yaml 或 json 文件
        for ext in ['.yaml', '.json']:
            config_file = self.config_dir / f"{config_name}{ext}"
            if config_file.exists():
                with open(config_file, 'r', encoding='utf-8') as f:
                    config = yaml.safe_load(f) if ext == '.yaml' else json.load(f)
                self._cache[config_name] = config
                return config
        
        raise FileNotFoundError(f"配置文件不存在: {config_name}")
    
    def get_llm_config(self) -> Dict[str, Any]:
        """获取LLM配置"""
        return self.load_config("llm_config")
    
    def get_mcp_config(self) -> Dict[str, Any]:
        """获取MCP配置"""
        return self.load_config("mcp_server")


# 全局配置管理器实例
config_manager = ConfigManager()

# 便捷函数
get_config = config_manager.load_config
get_llm_config = config_manager.get_llm_config
get_mcp_config = config_manager.get_mcp_config
